package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commen.BaseContext;
import com.itheima.reggie.commen.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.impl.CategoryServiceImpl;
import com.itheima.reggie.service.impl.DishServiceImpl;
import com.itheima.reggie.service.impl.SetmealServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
//    @Autowired
//    private DishServiceImpl dishService;
//    @Autowired
//    private SetmealServiceImpl setmealService;
    @PostMapping
    public R<String> addCategory(@RequestBody Category category, HttpServletRequest request){
        Long empID = (Long) request.getSession().getAttribute("employee");
//        category.setCreateUser(empID);
//        category.setUpdateUser(empID);
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
        BaseContext.setCurrentId(empID);
        categoryService.save(category);
        return R.success("添加成功..");
    }

    @GetMapping("/page")
    public R<Page> selectPage(Integer page, Integer pageSize){
        Page pageInfo = new Page(page,pageSize);
        categoryService.page(pageInfo);
        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> deleteById(Long id){
//        QueryWrapper qw = new QueryWrapper();
//        qw.eq("category_id",id);
//        int dishCount = dishService.count(qw);
//        int setmealCount = setmealService.count(qw);
//        if (dishCount > 0) {
//            return R.error("当前分类下有菜品，不能删除");
//        }
//        if (setmealCount > 0) {
//            return R.error("当前分类下有套餐，不能删除");
//        }
        categoryService.remove(id);
        return R.success("删除成功..");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category, HttpServletRequest request){
        Long empID = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(empID);
        categoryService.updateById(category);
        return R.success("修改成功..");
    }

    @GetMapping("/list")
    public R<List<Category>> selectCategory(Category category){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getSort).eq(category.getType() != null, Category::getType,category.getType());
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }
}
